package jdbcForMe.handler;

import jdbcForMe.mySetting.MyFormSetting;
import jdbcForMe.mySetting.MyItemSetting;
import jdbcForMe.mySetting.SQLContent;
import jdbcForMe.setting.ItemSetting;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class DBChanger implements Changer {
    Connection connection;

    /**
     * @param conn 数据库的Connection对象
     */
    public DBChanger(Connection conn) {
        connection = conn;
    }


    public void update(String tableName, MyFormSetting mfs) throws SQLException {
        Statement s = connection.createStatement();
        s.execute("truncate table " + tableName + " ;");
        PreparedStatement ps = connection.prepareStatement("insert into " + tableName + new SQLContent() {
            @Override
            public String createString() {
                return "(id , name)";
            }
        }.createString() + " values ( ? , ? )");//函数式接口实现自定义SQL语句
        while(mfs.hasNext()){
            MyItemSetting is = (MyItemSetting) mfs.next();
            ps.setInt(1, (Integer) is.getId());
            ps.setString(2, is.getName());
            ps.execute();
        }
    }

    // 删除
    // 修改
    @Override
    public void dispose() {

    }
}
